public class MyLinkedList {

    static class ListNode{
        int val;
        ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
    public int Size;

    public MyLinkedList() {
        this.head = null;
    }

    public int get(int index) {
        if(index<0||index>=Size){
            return -1;
        }else{
            ListNode cur = this.head;
            while(index>0){
                cur = cur.next;
                index--;
            }
            return cur.val;
        }
    }

    public void addAtHead(int val) {
        ListNode cur = new ListNode(val);
        cur.next = head;
        this.head = cur;
        this.Size++;
    }

    public void addAtTail(int val) {
        if(this.head == null){
            this.head = new ListNode(val);
            this.Size++;
            return ;
        }
        ListNode cur = head;
        while(cur.next!=null){
            cur = cur.next;
        }
        ListNode newnode = new ListNode(val);
        cur.next = newnode;
        this.Size++;
    }

    public void addAtIndex(int index, int val) {
        if(index<=0){
            addAtHead(val);
            return ;
        }else if(index>this.Size){
            return ;
        }else if(index==this.Size){
            addAtTail(val);
            return ;
        }
        else{
            ListNode cur = this.head;
            while(index-1>0&&cur.next!=null){
                index--;
                cur = cur.next;
            }
            ListNode newnode = new ListNode(val);
            newnode.next = cur.next;
            cur.next = newnode;
            this.Size++;
        }
    }

    public void deleteAtIndex(int index) {
        if(index<0||index>=Size){
            return ;
        }else if(index==0){
            this.head = this.head.next;
            this.Size--;
            return ;
        }else{
            ListNode cur = this.head;
            while(cur.next!=null&&index-1>0){
                cur = cur.next;
                index--;
            }
            cur.next = cur.next.next;
            this.Size--;
            return ;
        }
    }
}